<?php
/* 
 * This is the superclass that controls the business model entities
 * Business model objects inherit this class' funtionality but
 * are not aware of it
 * this is the main contact from the Domain model and the application
 */

class BusinessObject
{
    /**
     * List of all observers
     * @var <array>
     */
    private $adapters = array();

    /**
     * add an observer
     *
     * @param BusinessAdapter $adapter
     */
    public function addAdapter(BusinessAdapter $adapter){
        $class = get_class($adapter);
        if(!array_key_exists($class, $this->adapters)){
            $this->adapters[$class] = $adapter;
        }
    }

    /**
     *  call the update mthod to all adapters
     * 
     */
    public function update(){
        foreach($this->adapters as $adapter){
            $adapter->update($this, $arguments);
        }
    }

    /**
     * Overload methods __set and __get are not working with protected
     * propreties of the child class
     *
     *
     * @param <type> $name
     * @param <type> $arguments 
     */
    public function __call($name, $arguments){
        echo "calling: ".$name.PHP_EOL;
        if(substr($name, 0, 3) == 'set'){
            $this->set(strtolower(substr($name, 3)), current($arguments));
        }
        elseif(substr($name, 0, 3) == 'get'){
            return $this->get(strtolower(substr($name, 3)), current($arguments));
        }
        else{
            throw new exception("Method $name is not implemented in class ".get_class($this));
        }
    }

    /**
     * generic get method
     *
     * @param <type> $name
     * @param <type> $value
     */
    public function set($name, $value){
        $this->$name = $value;
    }

    /**
     * generic set method
     *
     * @param <type> $name
     * @return <type>
     */
    public function get($name){
        return $this->$name;
    }


}
